<html>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<p><font color="#0000FF"><a href="../servlet/JndiServlet"><img src="../images/execute.gif" align="right" border="0"></a><a href="index.html"><img src="../images/return.gif" width="24" height="24" align="right" border="0"></a></font></p>
<h3>Source Code for Naming Example<font color="#0000FF"><br>
  </font> </h3>
<font color="#0000FF"></font> 
<pre>
<font color="#0000FF">import</font> java.io.IOException;
<font color="#0000FF">import</font> java.io.PrintWriter;
<font color="#0000FF">import</font> java.util.Enumeration;
<font color="#0000FF">import</font> java.util.Hashtable;
<font color="#0000FF">import</font> javax.servlet.*;
<font color="#0000FF">import</font> javax.servlet.http.*;
<font color="#0000FF">import</font> javax.naming.NamingException;
<font color="#0000FF">import</font> javax.naming.Context;
<font color="#0000FF">import</font> javax.naming.InitialContext;
<font color="#0000FF">import</font> javax.naming.NamingEnumeration;
<font color="#0000FF">import</font> javax.naming.directory.InitialDirContext;

<font color="#CC0000">/**
 * Demonstration of the web application environment support.
 *
 * @author Remy Maucherat
 */</font>

<font color="#0000FF">public class</font> JndiServlet 
    <font color="#0000FF">extends</font> HttpServlet {


    <font color="#0000FF">public void</font> doGet(HttpServletRequest request, HttpServletResponse response)
        <font color="#0000FF">throws</font> ServletException, IOException {
        
        PrintWriter out = response.getWriter();
        response.setContentType(&quot;<font color="#009900">text/plain</font>&quot;);
        
        Context ctx = null;
        
        <font color="#0000FF">try</font> {
            ctx = new InitialContext();
        } <font color="#0000FF">catch</font> (NamingException e) {
            out.println(&quot;<font color="#009900">Couldn't build an initial context : </font>&quot; + e);
            return;
        }
        
        <font color="#0000FF">try</font> {
            Object value = ctx.lookup(&quot;<font color="#009900">java:/comp/env/maxExemptions)</font>&quot;;
            out.println(&quot;<font color="#009900">Simple lookup test : </font>&quot;);
            out.println(&quot;<font color="#009900">Max exemptions value : </font>&quot; + value);
        } <font color="#0000FF">catch</font> (NamingException e) {
            out.println(&quot;<font color="#009900">JNDI lookup failed : </font>&quot; + e);
        }
        
        <font color="#0000FF">try</font> {
            Context envCtx = (Context) ctx.lookup(&quot;<font color="#009900">java:/comp/env/</font>&quot;);
            out.println(&quot;<font color="#009900">list() on /comp/env Context : </font>&quot;);
            NamingEnumeration enum = ctx.list(&quot;<font color="#009900">java:/comp/env/</font>&quot;);
            while (enum.hasMoreElements()) {
                out.print(&quot;<font color="#009900">Binding : </font>&quot;);
                out.println(enum.nextElement().toString());
            }
            out.println(&quot;<font color="#009900">listBindings() on /comp/env Context : </font>&quot;);
            enum = ctx.listBindings(&quot;<font color="#009900">java:/comp/env/</font>&quot;);
            while (enum.hasMoreElements()) {
                out.print(&quot;<font color="#009900">Binding : </font>&quot;);
                out.println(enum.nextElement().toString());
            }
        } <font color="#0000FF">catch</font> (NamingException e) {
            out.println(&quot;<font color="#009900">JNDI lookup failed : </font>&quot; + e);
        }
        
    }
    
    
}
</pre>
</body>
</html>
